package com.nbjtjc.safe.service.danger;

import cn.hutool.core.collection.CollectionUtil;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.mapper.danger.DangerReformSheetMapper;
import com.nbjtjc.safe.mapper.sys.SysFileMapper;
import com.nbjtjc.safe.model.danger.DangerReformSheet;
import com.nbjtjc.safe.model.sys.SysFile;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/6/26 8:15
 **/
@Service
public class DangerReformSheetService {

    @Autowired
    private DangerReformSheetMapper dangerReformSheetMapper;

    @Autowired
    private SysFileMapper sysFileMapper;
    public List<DangerReformSheet> find(DangerReformSheet dangerReformSheet){
        List<DangerReformSheet> dangerReformSheets=dangerReformSheetMapper.select(dangerReformSheet);
        compareDangerReformSheet(dangerReformSheets);
        return dangerReformSheets;
    }

    public DangerReformSheet add(DangerReformSheet dangerReformSheet){
        dangerReformSheet.setCreateDate(new Date());
        dangerReformSheet.setCreateUserId(MyShiroUtils.getSessionUserId());
        dangerReformSheetMapper.insertSelective(dangerReformSheet);
        return dangerReformSheet;
    }

    public DangerReformSheet update(DangerReformSheet dangerReformSheet){
        dangerReformSheetMapper.updateByPrimaryKeySelective(dangerReformSheet);
        return dangerReformSheet;
    }


    /**
     * 加载检查单的临时措施照片，整改附件
     * @param dangerReformSheets
     */
    private void compareDangerReformSheet(List<DangerReformSheet> dangerReformSheets){
        if(CollectionUtil.isEmpty(dangerReformSheets)){
            return;
        }
        dangerReformSheets.parallelStream().forEach(dangerReformSheet -> {
            SysFile sysFile=new SysFile();
            sysFile.setContentId(dangerReformSheet.getId());
            sysFile.setContentType(Constant.SysFileType.REFORM_SHEET_ATAACHMENT.getValue());
            List<SysFile> sysFiles=sysFileMapper.select(sysFile);
            if(CollectionUtil.isNotEmpty(sysFiles)){
                dangerReformSheet.setReformAttachments(sysFiles);
            }
            sysFile.setContentType(Constant.SysFileType.SUPERVISE_ATAACHMENT.getValue());
            List<SysFile> superviseFiles=sysFileMapper.select(sysFile);
            if(CollectionUtil.isNotEmpty(superviseFiles)){
                dangerReformSheet.setSuperviseAttachments(superviseFiles);
            }
            sysFile.setContentType(Constant.SysFileType.TEMP_MEASURE_PIC.getValue());
            List<SysFile> tempMeasurePics=sysFileMapper.select(sysFile);
            if(CollectionUtil.isNotEmpty(tempMeasurePics)){
                dangerReformSheet.setMeasurePics(tempMeasurePics);
            }
        });

    }

}
